package com.smasher.stepview

import android.content.Context
import android.util.AttributeSet
import android.view.View.OnClickListener
import android.widget.LinearLayout
import androidx.appcompat.widget.AppCompatButton
import androidx.core.content.ContextCompat
import androidx.core.content.withStyledAttributes

class StepPanel : LinearLayout {

    private val buttonPrevious: AppCompatButton
    private val buttonAction: AppCompatButton

    private var buttonSpacing = 0
    private var pageSize = 4
    private var currentStep = 1

    private var listener: PanelListener? = null

    constructor(context: Context) : this(context, null)

    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : this(
        context,
        attrs,
        defStyleAttr,
        0
    )

    constructor(
        context: Context,
        attrs: AttributeSet?,
        defStyleAttr: Int,
        defStyleRes: Int
    ) : super(context, attrs, defStyleAttr, defStyleRes) {
        orientation = HORIZONTAL
        // 初始化属性集，这里可以根据需要添加自定义属性解析逻辑
        context.withStyledAttributes(attrs, R.styleable.StepPanel) {
            // 解析自定义属性（如果有的话）
            buttonSpacing = getDimensionPixelSize(R.styleable.StepPanel_buttonSpacing, 0)
            pageSize = getInt(R.styleable.StepPanel_pageSize, 4)
            currentStep = getInt(R.styleable.StepPanel_currentStep, 1)
        }

        buttonPrevious = AppCompatButton(context).apply {
            text = context.getString(R.string.step_panel_previous)
            setTextColor(
                ContextCompat.getColorStateList(
                    context,
                    R.color.step_panel_color_previous_state
                )
            )
            layoutParams = LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f).apply {
                // 设置左边距以实现按钮间的间距
                marginEnd = buttonSpacing / 2
            }
            background =
                ContextCompat.getDrawable(
                    context,
                    R.drawable.selector_step_panel_previous_background
                )

        }
        addView(buttonPrevious)
        buttonPrevious.setOnClickListener(onPreviousClickListener)

        buttonAction = AppCompatButton(context).apply {
            text = context.getString(R.string.step_panel_next)
            setTextColor(
                ContextCompat.getColorStateList(
                    context,
                    R.color.step_panel_color_next_state
                )
            )
            layoutParams = LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f).apply {
                // 设置左边距以实现按钮间的间距
                marginStart = buttonSpacing / 2
            }
            background =
                ContextCompat.getDrawable(context, R.drawable.selector_step_panel_next_background)

        }
        addView(buttonAction)
        buttonAction.setOnClickListener(onNextClickListener)

        applyButtonStatus()
    }

    private fun nextPage() {
        if (currentStep < pageSize) {
            this.currentStep++
        }
        applyButtonStatus()
    }

    private fun previousPage() {
        if (currentStep > 1) {
            this.currentStep--
        }
        applyButtonStatus()
    }

    private fun applyButtonStatus() {
        buttonPrevious.isEnabled = currentStep != 1
        buttonAction.text = if (currentStep == pageSize) {
            context.getString(R.string.step_panel_submit)
        } else {
            context.getString(R.string.step_panel_next)
        }
    }

    private val onPreviousClickListener = OnClickListener {
        val result = listener?.onPrevious(currentStep, currentStep - 1) ?: false
        if (result) {
            previousPage()
            listener?.onPageChanged(currentStep)
        }
    }

    private val onNextClickListener = OnClickListener {
        if (currentStep == pageSize) {
            listener?.onSubmit(currentStep)
        } else {
            val result = listener?.onNext(currentStep, currentStep + 1) ?: false
            if (result) {
                nextPage()
                listener?.onPageChanged(currentStep)
            }
        }
    }

    fun setPaneListener(listener: PanelListener) {
        this.listener = listener
    }

    interface PanelListener {
        fun onPrevious(current: Int, target: Int): Boolean
        fun onNext(current: Int, target: Int): Boolean
        fun onSubmit(current: Int)
        fun onPageChanged(current: Int)
    }

    companion object {
        const val TAG = "StepPanel"
    }
}
